package com.avic.modules.backup4Dameng;

import com.avic.modules.backup4Dameng.remote.ScpCopyExecute;
import com.avic.modules.backup4Dameng.vo.FileVo;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Slf4j
public class DmpBackup {

    private final ScpCopyExecute scpCopyExecute;

    private static final String SEP = File.separator;
    public static String DMP_EXTERNAL_DIR = System.getProperty("user.home") + SEP + ".ehs" + SEP + "file" + SEP + "tmp" + SEP + "external" + SEP;
    public static String DMP_EXTENSION = ".dmp";
    public static int MAGIC_SEVEN = 7;
    public static String DATE_DETAIL_FORMAT = "yyyy.MM.dd HH:mm:ss";

    private String dexpPath;
    private String dexpURL;
    private String exportDirectory;
    private String winAdmin;
    private String winAdminPass;
    private String addr;

    public DmpBackup(ScpCopyExecute scpCopyExecute) {
        this.scpCopyExecute = scpCopyExecute;
    }

    public void init(String dexpPath,String dexpURL,String exportDirectory,String winAdmin,String winAdminPass,String addr){
        this.dexpPath = dexpPath;
        this.dexpURL = dexpURL;
        this.exportDirectory = exportDirectory;
        this.winAdmin = winAdmin;
        this.winAdminPass = winAdminPass;
        this.addr = addr;
    }

    /**
     * 7 天备份 seven eleven
     */
    public static void seven11(String sqlDir) {
        File sqlFile = new File(sqlDir);
        if (sqlFile.isDirectory()) {
            File[] files = sqlFile.listFiles();
            for (File file : files) {
                if (!file.getAbsolutePath().endsWith(DMP_EXTENSION)) {
                    file.delete();
                    files = sqlFile.listFiles();
                }
            }
            if (files.length > MAGIC_SEVEN) {
                Arrays.sort(files, (f1, f2) -> {
                    long deltaDiff = f1.lastModified() - f2.lastModified();
                    if (deltaDiff > 0) {
                        return 1;
                    } else if (deltaDiff == 0) {
                        return 0;
                    } else {
                        return -1;
                    }
                });
                for (int i = 0; i < files.length - MAGIC_SEVEN; i++) {
                    files[i].delete();
                }
            }
        } else {
            throw new RuntimeException("【 The file is not folder or dir ,check with ops】");
        }

    }

//    /**
//     * 此方法用来导出
//     */
//    public String exportDmp() {
//        String dmpFileName = scpCopyExecute.exportDmp(addr,dexpPath,dexpURL,exportDirectory,winAdmin, winAdminPass);
//        seven11(DMP_EXTERNAL_DIR);
//        return dmpFileName;
//    }


    public List<FileVo> getDmpFiles() {
        File dmpDirectory = new File(DMP_EXTERNAL_DIR);
        if (dmpDirectory.isDirectory()) {
            HashMap<Long, String> sqlPathMap = new HashMap<>();
            File[] files = dmpDirectory.listFiles();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_DETAIL_FORMAT);
            List<FileVo> fileVoList = new ArrayList<>();
            for (int i = files.length - 1; i >= 0; i--) {
                if(!files[i].isDirectory() && files[i].getName().endsWith(DMP_EXTENSION)) {
                    String mt = String.valueOf(files[i].lastModified());
                    FileVo fileVo = new FileVo();
                    fileVo.setMtime(Long.parseLong(mt.substring(0, mt.length() - 3)));
                    fileVo.setPathName(files[i].getAbsolutePath());
                    fileVoList.add(fileVo);
                }else{
                    System.out.println(files[i].getName());
                }
            }
            fileVoList.sort((o1, o2) -> o2.getMtime().compareTo(o1.getMtime()));
            return fileVoList;
        }
        return null;
    }

    private static String formatEpoch(long epoch){
        LocalDateTime dateTime = LocalDateTime.ofEpochSecond(epoch / 1000L, 0, ZoneOffset.ofHours(8));
        return dateTime.format(DateTimeFormatter.ofPattern(DATE_DETAIL_FORMAT));
    }
}
